﻿using Microsoft.EntityFrameworkCore;
using System.Reflection.Emit;
using Volo.Abp.AuditLogging;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.PermissionManagement;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace YDT_ABPvNext_Projects.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class YDT_ABPvNext_ProjectsDbContext :
    AbpDbContext<YDT_ABPvNext_ProjectsDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    #region Entities from the modules

    /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityDbContext and ITenantManagementDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    //Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }
    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    #endregion

    public YDT_ABPvNext_ProjectsDbContext(DbContextOptions<YDT_ABPvNext_ProjectsDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        //builder.ConfigurePermissionManagement();
        //builder.ConfigureAuditLogging();
        //builder.ConfigureIdentity();
        //builder.ConfigureTenantManagement();

        ////配置应用权限模块
        //模型表前缀
        //base.OnModelCreating(modelBuilder);

        ////配置应用权限模块
        //模型表前缀
        AbpPermissionManagementDbProperties.DbTablePrefix = "YDT_";
        builder.ConfigurePermissionManagement();

        //modelBuilder.Entity(delegate (EntityTypeBuilder<Zdf_test> b)
        //{
        //    b.ToTable(AbpPermissionManagementDbProperties.DbTablePrefix + "xxwwwwxxxxww", AbpPermissionManagementDbProperties.DbSchema);
        //    b.ConfigureByConvention();
        //    b.Property((Zdf_test x) => x.Id).HasMaxLength(PermissionDefinitionRecordConsts.MaxNameLength).IsRequired();
        //    b.Property((Zdf_test x) => x.name).HasMaxLength(PermissionGrantConsts.MaxProviderNameLength).IsRequired();
        //    b.ApplyObjectExtensionMappings();
        //});

        //可以在此处配置表别名
        //modelBuilder.Entity(delegate (EntityTypeBuilder<SysUser> b)
        //{
        //    b.ToTable(AbpPermissionManagementDbProperties.DbTablePrefix + "users", AbpPermissionManagementDbProperties.DbSchema);
        //});

        //配置应用身份认证模块
        //模型表前缀
        AbpIdentityDbProperties.DbTablePrefix = "YDT_";
        builder.ConfigureIdentity();

        //配置应用租户模块
        AbpTenantManagementDbProperties.DbTablePrefix = "YDT_";
        builder.ConfigureTenantManagement();

        //配置审计日志模块
        AbpAuditLoggingDbProperties.DbTablePrefix = "YDT_";
        builder.ConfigureAuditLogging();

        //配置审计日志模块
        AbpAuditLoggingDbProperties.DbTablePrefix = "YDT_";
        builder.ConfigureAuditLogging();

        /* Configure your own tables/entities inside here */

        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(YDT_ABPvNext_ProjectsConsts.DbTablePrefix + "YourEntities", YDT_ABPvNext_ProjectsConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});
    }
}
